package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.MyPrizeInfoVo;
import com.sinoglobal.ningxia.http.ConnectionUtil;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

/**
 * 
 * @author zbl
 * @createdate 2014-7-28 下午1:47:54
 * @Description: 积分转盘“我的”的列表
 */
public class MyPrizeAdapter extends BaseAdapter {
	private ArrayList<MyPrizeInfoVo> myPrizeVo;
	private Context context;
	private FinalBitmap fb;

	public MyPrizeAdapter(ArrayList<MyPrizeInfoVo> myPrizeVo, Context context) {
		super();
		this.myPrizeVo = myPrizeVo;
		this.context = context;
		fb = FinalBitmap.create(context);
	}

	@Override
	public int getCount() {
		return myPrizeVo.size();
	}

	public ArrayList<MyPrizeInfoVo> getMyPrizeVo() {
		return myPrizeVo;
	}

	public void setMyPrizeVo(ArrayList<MyPrizeInfoVo> myPrizeVo) {
		this.myPrizeVo = myPrizeVo;
	}

	@Override
	public Object getItem(int position) {
		return myPrizeVo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final String RECEIVED="1";
		final String NO_RECEIVED="2";
		final String SENDED="3";
		final String SUBMITED="4";
		final String SPACING=" ";
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.my_prize_item, null);
			viewHolder = new ViewHolder();
			viewHolder.myPrizeItemTitle = (TextView) convertView.findViewById(R.id.my_prize_name);
			viewHolder.myPrizeItemImgUrl = (ImageView) convertView.findViewById(R.id.my_prize_item_img);
			viewHolder.myPrizeItemIsGone = (ImageView) convertView.findViewById(R.id.my_prize_item_isgone);
			viewHolder.myPrizeItemDate = (TextView) convertView.findViewById(R.id.my_prize_item_date);
			viewHolder.ismyPrize = (TextView) convertView.findViewById(R.id.my_prize_item_expiry_date_text);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}// 将传进来的myActivityVo中的imgUrl显示在imageview上
		

		fb.configLoadfailImage(R.drawable.default43);
		fb.configLoadingImage(R.drawable.default43);
		fb.display(viewHolder.myPrizeItemImgUrl, ConnectionUtil.localUrl + myPrizeVo.get(position).getImg());
		// 根据myActivityVo的is_late来判断该活动是否过期 0过期 1没过期, 为0的文字都要置灰
		if ("0".equals(myPrizeVo.get(position).getPull_status())) {
			viewHolder.myPrizeItemIsGone.setVisibility(View.VISIBLE);
			viewHolder.myPrizeItemTitle.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
			viewHolder.ismyPrize.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
			viewHolder.myPrizeItemDate.setTextColor(context.getResources().getColor(R.color.my_activity_item_text));
		} else {
			viewHolder.myPrizeItemIsGone.setVisibility(View.GONE);
			viewHolder.myPrizeItemTitle.setTextColor(context.getResources().getColor(R.color.shopping_goodsName_color));
			viewHolder.myPrizeItemDate.setTextColor(context.getResources().getColor(R.color.shopping_goodsName_color));
			viewHolder.ismyPrize.setTextColor(context.getResources().getColor(R.color.shopping_goodsName_color));
		}
		// 分别给活动的标题、日期、有效期赋值
		viewHolder.myPrizeItemTitle.setText(myPrizeVo.get(position).getPrize_name());
		// 对返回的日期做处理，返回的格式为2014-7-8 10:26:19，去掉空格
		int col_time_index = myPrizeVo.get(position).getZhong_time().indexOf(SPACING);
		String col_time = myPrizeVo.get(position).getZhong_time().substring(0, col_time_index);
		viewHolder.myPrizeItemDate.setText(col_time);
		if (myPrizeVo.get(position).getPrize_state().endsWith(RECEIVED)) {
			viewHolder.ismyPrize.setText(context.getString(R.string.received));
		} else if (myPrizeVo.get(position).getPrize_state().endsWith(NO_RECEIVED)) {
			viewHolder.ismyPrize.setText(context.getString(R.string.no_received));
		} else if (myPrizeVo.get(position).getPrize_state().endsWith(SENDED)) {
			viewHolder.ismyPrize.setText(context.getString(R.string.sended));
		} else if (myPrizeVo.get(position).getPrize_state().endsWith(SUBMITED)) {
			viewHolder.ismyPrize.setText(context.getString(R.string.submitted));
		}
		return convertView;
	}

	class ViewHolder {
		ImageView myPrizeItemImgUrl;// 图片的显示
		ImageView myPrizeItemIsGone;// 该条活动是否过期
		TextView myPrizeItemTitle;// 奖品名称
		TextView myPrizeItemDate;// 中奖的日期
		TextView ismyPrize;// 领奖状态
	}

	ViewHolder viewHolder = null;

}
